﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.QuickReplyItems.Service
{
    public class QuickReplyItemAppService : BaseAppService, IQuickReplyItemAppService
    {
        private IQuickReplyItemDomainService _domainService;
        private IQuickReplyItemVerifyDataManager _quickReplyItemVerifyManager;
        private IQuickReplyVerifyDataManager _quickReplyVerifyManager;

        public QuickReplyItemAppService(IQuickReplyItemDomainService domainService,
            IQuickReplyItemVerifyDataManager quickReplyItemVerifyManager,
            IQuickReplyVerifyDataManager quickReplyVerifyManager) : base()
        {
            this._domainService = domainService;
            this._quickReplyItemVerifyManager = quickReplyItemVerifyManager;
            this._quickReplyVerifyManager = quickReplyVerifyManager;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLYITEM, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            var errorInfo = _quickReplyItemVerifyManager.VerifyId(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLYITEM, AuditAction.CREATE)]
        public QuickReplyItemDto Add(Guid quickReplyId, QuickReplyItemCreateDto dto)
        {
            var errorInfo = _quickReplyVerifyManager.VerifyId(quickReplyId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            errorInfo = _quickReplyItemVerifyManager.VerifyCreate(dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            dto.QuickReplyId = quickReplyId;
            QuickReplyItem quickReplyItem = _domainService.Create(Mapper.Map<QuickReplyItem>(dto));
            return Mapper.Map<QuickReplyItemDto>(quickReplyItem);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLYITEM, AuditAction.UPDATE)]
        public QuickReplyItemDto Update(QuickReplyItemUpdateDto dto)
        {
            var errorInfo = _quickReplyItemVerifyManager.VerifyUpdate(dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            QuickReplyItem quickReplyItem = _domainService.Update(Mapper.Map<QuickReplyItemUpdateBo>(dto));
            return Mapper.Map<QuickReplyItemDto>(quickReplyItem);
        }
        public QuickReplyItemDto Get(Guid id)
        {
            var errorInfo = _quickReplyItemVerifyManager.VerifyId(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            QuickReplyItem quickReplyItem = _domainService.Get(id);
            return Mapper.Map<QuickReplyItemDto>(quickReplyItem);
        }
        public QuickReplyItemDto Get(Guid id, string include)
        {
            var errorInfo = _quickReplyItemVerifyManager.VerifyId(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            QuickReplyItem quickReplyItem = _domainService.Get(id, include);
            return Mapper.Map<QuickReplyItemDto>(quickReplyItem);
        }

        public IEnumerable<QuickReplyItemDto> GetList(Guid quickreplyId, string include)
        {
            var errorInfo = _quickReplyVerifyManager.VerifyId(quickreplyId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            IEnumerable<QuickReplyItem> list = _domainService.GetByQuickReplyId(quickreplyId, include);
            return Mapper.Map<IEnumerable<QuickReplyItemDto>>(list);
        }
    }
}
